<template>
  <view class="apply_bank_qrcode_step1 need_scroll_top_view">
    <steps :active="1"></steps>
    <view class="cut-line"></view>
    <scroll-view class="scroll-wrap" :scroll-y="true" :style="{height: (safeHeight - safeWidth / 375 * (101 + 64 + 11)) + 'px'}">
      <view class="form-wrap">
        <form>
          <view class="form-item self-border-bottom">
            <text class="form-item-label">门店名称</text>
            <input type="text" v-model="data.storeName" class="form-item-input" disabled placeholder="请填写您的门店经营名称" placeholder-class="input-placeholder" @blur="backFixationTop()"/>
            <view class="no-arrow"></view>
          </view>
          <!-- <view class="form-item self-border-bottom">
            <text class="form-item-label">商户类型</text>
            <picker :value="natureIndex" :range="storeNatureList" disabled range-key="name" @change="bindNaturePickerChange" class="form-item-pick">
              <view class="pick-value" v-if="storeNatureList && storeNatureList.length > 0 && selectNature">{{storeNatureList[natureIndex].name}}</view>
              <view class="pick-value" v-else>请选择商户类型</view>
            </picker>
            <view class="arrow"></view>
          </view> -->
          <view class="form-item">
            <text class="form-item-label">店铺面积</text>
            <input type="number" v-model="data.storeProportion" :disabled="isDisabled" class="form-item-input" placeholder="请填写您的店铺面积" placeholder-class="input-placeholder" @blur="backFixationTop()"/>
            <view class="no-arrow"></view>
          </view>
          <view class="cut-line"></view>
          
          <view class="form-item self-border-bottom">
            <text class="form-item-label">店铺地址</text>
            <picker :value="multiDistrictIndex" range-key="name" :range="multiDistrictArray" disabled @cancel="bindDistrictPickerCancel" @change="bindDistrictPickerChange" @columnchange="bindDistrictColumnPickerChange" class="form-item-pick" mode="multiSelector">
              <view class="pick-value" v-if="multiDistrictArray && multiDistrictArray.length > 0 && selectDistrict">
                <text v-if="multiDistrictArray[0] && multiDistrictArray[0][multiDistrictIndex[0]]">{{multiDistrictArray[0][multiDistrictIndex[0]].name}}</text>
                <text v-if="multiDistrictArray[1] && multiDistrictArray[1][multiDistrictIndex[1]]">{{multiDistrictArray[1][multiDistrictIndex[1]].name}}</text>
                <text v-if="multiDistrictArray[2] && multiDistrictArray[2][multiDistrictIndex[2]]">{{multiDistrictArray[2][multiDistrictIndex[2]].name}}</text>
              </view>
              <view class="pick-value" v-else>请选择店铺地址</view>
            </picker>
            <view class="arrow"></view>
          </view>
          <view class="form-item">
            <text class="form-item-label">门店地址</text>
            <input type="text" disabled v-model="data.address" class="form-item-input" placeholder="请选择您的门店地址" placeholder-class="input-placeholder" @blur="backFixationTop()"/>
            <view class="no-arrow"></view>
          </view>
          <view class="cut-line"></view>
          <view class="form-item-upload-wrap">
            <view class="form-item-head">
              <view class="title">
                门头照片
                <text class="tip">(必须包含牌匾和店外环境)</text>
              </view>
              <view class="sample" @click="previewImg(1)">
                <text>示例</text>
              </view>
            </view>
            <view class="form-item-content" style="display: flex;" @click="uploadFile(2)">
              <view class="common-img-wrap" v-if="!data.plateFileId || !data.plateFileUrl">
                <view class="no-upload-img">
                  <image src="/static/image/qrcode/camera.png" mode="" class="img"></image>
                  <view class="text">
                    上传店铺照片
                  </view>
                </view>
              </view>
              <view class="common-img-wrap" v-else>
                <view class="upload-img-wrap">
                  <image :src="data.plateFileUrl" mode="" class="upload-img"></image>
                  <!-- <view class="close" @click.stop="delImg(2, index)">
                    <image :src="$store.state.imgBaseUrl + '/cancel.png'">
                  </view> -->
                </view>
              </view>
            </view>
          </view>
          <view class="form-item-upload-wrap">
            <view class="form-item-head">
              <view class="title">
                店铺照片
                <text class="tip">(必须包含店铺定位+门头+前台/收银台)</text>
              </view>
              <view class="sample" @click="previewImg(2)">
                <text>示例</text>
              </view>
            </view>
            <view class="form-item-content" style="display: flex;">
              <view class="common-img-wrap" v-for="(item, index) in data.storeInList" :key="index">
                <view class="upload-img-wrap">
                  <image :src="item.url" mode="" class="upload-img"></image>
                  <view class="close" @click.stop="delImg(1, item)" v-if="!isDisabled">
                    <image :src="$store.state.imgBaseUrl + '/cancel.png'">
                  </view>
                </view>
              </view>
              <view class="common-img-wrap" @click="uploadFile(1)">
                <view class="no-upload-img">
                  <image src="/static/image/qrcode/camera.png" mode="" class="img"></image>
                  <view class="text">
                    上传店铺照片
                  </view>
                </view>
              </view>
            </view>
          </view> 
        </form>
      </view>
    </scroll-view>
    
    <view class="qrcode_selected_platform-footer">
      <button type="primary" class="qrcode_selected_platform-footer-btn" @click="submitInfo" v-if="!status || status == 3 ">下一步</button>
      <button type="primary" class="qrcode_selected_platform-footer-btn" @click="nextStep" v-else>下一步</button>
    </view>
  </view>
</template>

<script>
import formValid from '@/unit/graceChecker.js'
import steps from '../common/bankSteps.vue'
import { getDistrict } from '@/service/common/city.service.js'
import { multiDistrict, uploadFile, checkImage } from '@/unit/common.js'
import { getStoreNatureSyncApi, getStoreInfoSyncApi, saveStoreInfoApi, getStepStatusApi, getSubmitStoreInfoApi } from '@/service/qrCode/qrCode.service.js'
import { scrollToTargetPosition, setSafeAreaSize } from '@/mixin/index.mixin.js'
export default {
  mixins: [scrollToTargetPosition, setSafeAreaSize],
  components: {
    steps
  },
  data () {
    return {
      id: null, // 收款码ID
      status: null,
      tradeList: [], // 行业类型列表
      tradeIndex: 0,
      selectDistrict: false,
      multiDistrictIndex: [], // 省市区选择的index
      multiDistrictArray: [], // 省市区数据 Array[Object]
      multiDistrictIds: [], // 省市区ID
      originMultiDistrictIndex: [], // 原始数据 用于取消选择省市区，还原原始数据
      originMultiDistrictArray: [], // 原始数据 用于取消选择省市区，还原原始数据
      originMultiDistrictIds: [], // 原始数据 用于取消选择省市区，还原原始数据
      data: {
        storeCollectId: null,
        storeName: '', // 门店名称
        // storeType: null, // 商户类型
        // storeTypeName: null, // 商户类型名称
        storeProportion: '', // 店铺面积
        districts: [], // 店铺地址
        districtId: null,
        address: '', // 门店地址
        storeInList: [], // 店铺照片(必须包含牌匾和店内环境)
        storeInFileIds: [],
        plateFileId: null,// 店铺照片(必须包含店铺定位+门头+前台/收银台)
        plateFileUrl: ''
      }
    }
  },
  computed: {
    isDisabled: function () {
      return this.status == 1 || this.status == 2;
    }
  },
  mounted () {},
  methods: {
    previewImg (type) { // 查看示例图片
      switch (type) {
      case 1:
        checkImage([this.$store.state.imgBaseUrl + '/esg_img/s-2.png'], 0);
        break;
      case 2:
        checkImage([this.$store.state.imgBaseUrl + '/esg_img/s-3.png'], 0);
        break;
      default:
      }
    },
    getStepStatus () {
      getStepStatusApi({}, res => {
        if (res.status == 0) {
          this.status = res.data.status;
          this.data.storeCollectId = res.data.id;
          if (res.data && res.data.storeStatus == 1) {
            this.getInfo(res.data.id);
          } else {
            this.init();
          }
        }
      });
    },
    getInfo (id) {
      getSubmitStoreInfoApi({id: id}, res => {
        if (res.status == 0) {
          this.data.storeName = res.data.storeName;
          this.data.storeProportion = res.data.storeProportion;
          this.data.districts = res.data.districtIds;
          this.data.districtId = res.data.districtId;
          this.initDistrictData(this.data.districts);
          this.selectDistrict = true;
          this.data.address = res.data.address;
          this.data.storeInList = res.data.storeInFileList.map(item => {
            item = {
              id: item.fileId,
              url: item.url
            };
            return item;
          });
          this.data.storeInFileIds = res.data.storeInFileList.map(item => item.id);
          this.data.plateFileId = res.data.plateFile.id;
          this.data.plateFileUrl = res.data.plateFile.url;
        }
      }, null, {showErrorMsg: true});
    },
    delImg (type, index) {
      switch (type) {
      case 1:
        this.data.storeInList.splice(index, 1);
        break;
      default:
      }
    },
    async initDistrictData (district) { // 初始化省市区数据
      let {multiDistrictArray, multiDistrictIndex, multiDistrictIds} = await multiDistrict.initMultDistrictArray({
        district: district
      });
      this.multiDistrictArray = multiDistrictArray;
      this.multiDistrictIndex = multiDistrictIndex;
      this.multiDistrictIds = multiDistrictIds;
      this.originMultiDistrictArray = JSON.parse(JSON.stringify(multiDistrictArray));
      this.originMultiDistrictIndex = JSON.parse(JSON.stringify(multiDistrictIndex));
      this.originMultiDistrictIds = JSON.parse(JSON.stringify(multiDistrictIds));
    },
    bindDistrictPickerCancel (e) { // 取消省市区的选择
      this.multiDistrictArray = JSON.parse(JSON.stringify(this.originMultiDistrictArray));
      this.multiDistrictIndex = JSON.parse(JSON.stringify(this.originMultiDistrictIndex));
      this.multiDistrictIds = JSON.parse(JSON.stringify(this.originMultiDistrictIds));
    },
    bindDistrictPickerChange (e) { // 确认省市区的选择
      this.originMultiDistrictArray = JSON.parse(JSON.stringify(this.multiDistrictArray));      
      this.originMultiDistrictIndex = JSON.parse(JSON.stringify(this.multiDistrictIndex));
      this.originMultiDistrictIds = JSON.parse(JSON.stringify(this.multiDistrictIds));
      this.data.districtId = this.multiDistrictIds[this.multiDistrictIds.length - 1];
    },
    async bindDistrictColumnPickerChange (e) { // 省市区的列发生变化
      if (e.detail.column < this.multiDistrictArray.length - 1) {
        let {multiDistrictArray, multiDistrictIndex, multiDistrictIds} = await multiDistrict.bindDistrictColumnPickerChange({
          column: e.detail.column,
          index: e.detail.value,
          multiDistrictIndex: JSON.parse(JSON.stringify(this.multiDistrictIndex)),
          multiDistrictArray: JSON.parse(JSON.stringify(this.multiDistrictArray)),
        });
        this.multiDistrictArray = multiDistrictArray;
        this.multiDistrictIndex = multiDistrictIndex;
        this.multiDistrictIds = multiDistrictIds;
      } else {
        this.multiDistrictIndex.splice(e.detail.column, 1, e.detail.value);
        this.multiDistrictIds.splice(e.detail.column, 1, this.multiDistrictArray[e.detail.column][e.detail.value].id);
      }
    },
    bindNaturePickerChange (e) {
      this.selectNature = true;
      this.natureIndex = e.detail.value;
      // this.data.storeTypeName = this.storeNatureList[e.detail.value].name;
    },
    uploadFile (type) {
      if (this.status == 1 || this.status == 2) {
        return false;
      }
      uploadFile({
        success: (res) => {
          switch (type) {
          case 1:
            this.data.storeInList.push({
              fileId: res.data.id,
              url: res.data.url
            });
            this.data.storeInFileIds.push(res.data.id);
            break;
          case 2:
            this.data.plateFileUrl = res.data.url;
            this.data.plateFileId = res.data.id;
            break;
          default:
          }
        }
      });
    },
    nextStep () {
      uni.navigateTo({
        url: '/pages/qrCode/applyBank/step2'
      })
    },
    submitInfo () {
      let rule = [
        { name:'storeProportion', checkType: 'notnull', checkRule: '',  errorMsg: '请填写门店面积' },
        { name:'storeInFileIds', checkType: 'notnull', checkRule: '',  errorMsg: '请选择店铺照片' },
        { name:'plateFileId', checkType: 'notnull', checkRule: '',  errorMsg: '请选择店铺照片' },
      ];
      var checkRes = formValid.check(this.data, rule);
      if (checkRes) {
        let params = Object.assign(this.data);
        params.storeInFileIds = params.storeInFileIds;
        delete params.storeInList;
        delete params.plateFileUrl;
        delete params.districts;
        saveStoreInfoApi(params, res => {
          if (res.status == 0) {
            uni.navigateTo({
              url: '/pages/qrCode/applyBank/step2'
            })
          }
        }, null, {showErrorMsg: true});  
      } else {
        uni.showToast({ title: formValid.error, icon: "none" });
      }
    },
    async init (option) {
      let storeInfo = await getStoreInfoSyncApi({});
      if (storeInfo) {
        this.data.storeName = storeInfo.name;
        this.data.address = storeInfo.address;
        // this.data.storeCollectId = storeInfo.id;
      }
      if (storeInfo.districts && storeInfo.districts.length > 0) {
        this.data.districts = storeInfo.districts;
        this.initDistrictData(this.data.districts);
        this.data.districtId = this.data.districts[this.data.districts.length - 1];
        this.selectDistrict = true;
      } else {
        this.initDistrictData();
      }
    }
  },
  mounted() {
    this.getStepStatus();
  },
  onShow () {
    // 
  },
  onLoad (option) {
    // this.id = option.id || null;
  }
}
</script>

<style lang="scss" scoped>
  .apply_bank_qrcode_step1 {
    position: relative;
    height: calc(100vh);
    background-color: #FFFFFF;
    .cut-line {
      height: 22rpx;
      background-color: #FAFAFA;
    }
    .scroll-wrap {
      height: calc(100vh - 62px - 101px);
      // background-color: #FFFFFF;
    }
    .form-wrap {
      // margin-top: 20rpx;
      background: #FFFFFF;
      .self-border-bottom {
        border-bottom: 1rpx solid #E1E1E1;
      }
      .self-margin-top {
        margin-top: 22rpx;
      }
      .cut-line {
        height: 22rpx;
        background-color: #FAFAFA;
      }
      
      .pick-value {
        color: #999999;
        font-size: 24rpx;
      }
      .form-item {
        display: flex;
        width: 718rpx;
        // justify-content: center;
        align-items: center;
        margin-left: 32rpx;
        // height: 100rpx;
        padding: 36rpx 0;
        
        .form-item-label {
          width: 140rpx;
          text-align: left;
          padding: 0 32rpx 0 0;
          font-size: 28rpx;
          color: #333333;
          font-weight:500;
        }
        .form-item-input {
          font-size: 24rpx;
          height: 38rpx;
          color: #999999;
          flex: 1;
          .input-placeholder {
            color: #999999;
          }
        }
        .form-item-pick {
          flex: 1;
        }
        .arrow {
          margin-right: 33rpx;
          border-right: 3rpx solid #8A8A8A;
          border-top: 3rpx solid #8A8A8A;
          height: 18rpx;
          width: 18rpx;
          transform: rotate(45deg);
          -webkit-transform: rotate(45deg);
          /*不加这两个属性三角会比上一个略丑, 大家可以试一下*/
          border-left: 2rpx solid transparent;
          border-bottom: 2rpx solid transparent;
        }
        .no-arrow {
          margin-left: 64rpx;
        }
      }
      
      .form-item-upload-wrap {
        background-color: #FFFFFF;
        padding: 0 34rpx 22rpx;
        .form-item-head {
          overflow: hidden;
          padding-top: 40rpx;
          display: flex;
          justify-content: space-between;
          align-items: center;
          .title {
            // float: left;
            color: #333333;
            font-size: 36rpx;
          }
          .sample {
            // float: right;
            text-align: right;
            width: 80rpx;
            text {
              color: #FF7473;
              font-size: 28rpx;
              text-decoration: underline;
            }
          }
          &:after {
            clear: both;
          }
        }
        .form-item-content {
          display: flex;
          flex-wrap: wrap;
          justify-content: space-between;
          .common-img-wrap {
            margin-top: 30rpx;
            width: 330rpx;
            height: 200rpx;
            border: 1px dashed #E1E1E1;
            border-radius: 16rpx;
           
            .no-upload-img {
              width: 100%;
              height: 100%;
              display: flex;
              flex-flow: column;
              align-items: center;
              justify-content: center;
              .img {
                width: 80rpx;
                height: 80rpx;
              }
              .text {
                margin-top: 29rpx;
                color: #666666;
                font-size: 24rpx;
                font-weight:500;
              }
            }
            
            .upload-img-wrap {
              width: 100%;
              height: 100%;
              position: relative;
              .upload-img {
                width: 100%;
                height: 100%;
                border-radius: 16rpx;
              }
              .close {
                position: absolute;
                width: 32rpx;
                height: 32rpx;
                top: -20rpx;
                right: -20rpx;
                image {
                  width: 100%;
                  height: 100%;
                }
              }
            }
          }
        }
      }
      
      .tip {
        font-size: 22rpx;
        font-family:PingFang SC;
        color: #999999;
        margin-left: 10rpx;
      }
    }
    
    .qrcode_selected_platform-footer {
      position: absolute;
      bottom: 0rpx;
      left: 0rpx;
      width: 100%;
      height: 125rpx;
      text-align: center;
      display: flex;
      align-items: center;
      justify-content: center;
      border-top: 1rpx solid #E1E1E1;
      background: #FFFFFF;
      .qrcode_selected_platform-footer-btn {
        width: 687rpx;
        height: 90rpx;
        line-height: 90rpx;
        background-color: #434343;
        color: #F6DA99;
        font-size: 30rpx;
      }
    }
    
  }
</style>
